// 1. 引入函数
import { defineStore } from 'pinia';
import { reqGetCategory1ListData, reqGetCategory2ListData, reqGetCategory3ListData } from '@/api/product/categorySelector'
import type { categoryInfoModel } from '@/api/product/model/categorySelectorModel';

// 2. 创建模块实例对象
export const useCategorySelectorInfoStore = defineStore('categorySelectorInfo', {
  state: () => {
    return {
      // 需要三个数组 分别存储各种分类数据
      category1List: [],
      category2List: [],
      category3List: [],
      // 需要三个id 后面会用到
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined
    }
  },
  actions: {
    async getCategory1ListData() {
      const result = await reqGetCategory1ListData()
      // console.log('c1',result)
      this.category1List = result
    },
    async getCategory2ListData(category1Id: number) {
      // 给category1Id属性赋值
      this.category1Id = category1Id
      const result = await reqGetCategory2ListData(category1Id)
      // console.log('c1',result)
      this.category2List = result

      // 重置二级三级分类数据 在一级分类更改的时候 二级三级数据恢复重置状态
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
    },
    async getCategory3ListData(category2Id: number) {
      // 给category1Id属性赋值
      this.category2Id = category2Id
      const result = await reqGetCategory3ListData(category2Id)
      // console.log('c1',result)
      this.category3List = result

      // 重置三级分类数据
      this.category3Id = undefined
    },
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    }
  }
})